package me.jiatao.wan.common.ext

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.view.LayoutInflater
import android.view.View
import com.google.android.material.button.MaterialButton
import com.nex3z.flowlayout.FlowLayout
import me.jiatao.wan.R
import me.jiatao.wan.data.network.pojo.CategoryTreePojo


fun Context.dip2px(dipValue: Float): Int {
    val scale: Float = resources.displayMetrics.density
    return (dipValue * scale + 0.5f).toInt()
}

fun Context.px2dip(pxValue: Float): Int {
    val scale: Float = resources.displayMetrics.density
    return (pxValue / scale + 0.5f).toInt()
}


@Suppress("DEPRECATION")
@SuppressLint("MissingPermission")
fun Context.isConnectedNetwork(): Boolean = run {
    val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val activeNetwork: NetworkInfo? = cm.activeNetworkInfo
    activeNetwork?.isConnectedOrConnecting == true
}

fun FlowLayout.addViewList(list: List<String>) {
    list.forEach { str ->
        val button = LayoutInflater.from(context)
            .inflate(R.layout.item_label_button, this, false) as MaterialButton
        button.text = str
        addView(button)
    }
}